package imageviewer.beans;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import imageviewer.persistence.Database;

public class AuthorBean extends BasicBean  {

	boolean authorized = false;

	String username = null;

	String password = null;

	Long id = null;
	
	public Long getId() {
		return id;
	}



	public void setId(Long id) {
		this.id = id;
	}



	public boolean isAuthorized() {
		return authorized;
	}



	public void setAuthorized(boolean authorized) {
		this.authorized = authorized;
	}



	public String getPassword() {
		return password;
	}



	public void setPassword(String password) {
		this.password = password;
	}



	public String getUsername() {
		return username;
	}



	public void setUsername(String username) {
		this.username = username;
	}



	public void authorize() {
		try {
			Connection con = Database.getConnection();
			
			Statement stmt = con.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT user_id FROM Users WHERE name='"
							+ username + "' AND pass=md5('" + password + "')");
			authorized = false;
			if (rs.next()) {
				authorized = true;
				id = rs.getLong(1);
			} 
			if(!authorized)
				super.setUserMessage("Login Error! Invalid credentials!");
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
